import { Provide} from '@midwayjs/core';
import { User} from '../entity/user/user';
import { Announcement } from '../entity/user/announcement';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';


@Provide()
export class UserService {

  @InjectEntityModel(User)
  userModel:Repository<User>
  
  async getuserinfo(openid: string):Promise<User>{
    try{
      const result = await this.userModel.findOneBy({openid})
      return result
    }catch(err){
      console.log("err",err);
      
    }
  }

  async adduser(user:User):Promise<User>{
    try{
      const result = await this.userModel.save(user);
      return result
    }catch(err){
      console.log("err",err);
      
    }
  }

  async updateuser(user:User):Promise<User>{
    const { openid, username, sex, phone, address } = user
    try{
      const Update = await this.userModel.findOneBy({openid});
      Update.username= username;
      Update.sex= sex;
      Update.phone= phone;
      Update.address= address;
      const result = await this.userModel.save(Update)
      return result
    }catch(err){
      console.log("err",err);
      
    }
  }
  

  @InjectEntityModel(Announcement)
  announcementModel:Repository<Announcement>

  async getannouncement():Promise<Announcement[]>{
    try{
      const result = await this.announcementModel.find()
      return result
    }catch(err){
      console.log("err",err);
      
    }
  }
}
